package com.topvision.common.security;

import com.topvision.utils.reflect.BeanMapper;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.stereotype.Component;

import java.security.Principal;
import java.util.LinkedHashMap;
import java.util.Map;

@Component
public class UserInfoUtils {

    public UserInfo getUserInfo(Principal principal){
        OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) principal;
        Authentication authentication = oAuth2Authentication.getUserAuthentication();
        Map<String, Object> details = new LinkedHashMap<>();
        details = (Map<String, Object>) authentication.getDetails();
        Map<String, Object> principalmap = (Map<String, Object>)details.get("principal");
        UserInfo userInfo = null;
        try {
            userInfo = (UserInfo) BeanMapper.mapToObject(principalmap,UserInfo.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  userInfo;
    }
}
